package com.example.xutils1;

import java.util.HashMap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

import com.example.xutils1.fragment.BuyFragment;
import com.example.xutils1.fragment.GoodFragment;
import com.example.xutils1.fragment.JiuJiuFragment;
import com.example.xutils1.fragment.LeftFragment;
import com.example.xutils1.fragment.MeFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class MainActivity extends FragmentActivity implements OnCheckedChangeListener {

	private LeftFragment mLeftFragment;

	private SlidingMenu mSlidingMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initSlidingMenu();

		mLeftFragment = new LeftFragment();

		getSupportFragmentManager().beginTransaction().replace(R.id.left_fgm, mLeftFragment).commit();

		((RadioButton) findViewById(R.id.tab_1)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.tab_2)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.tab_3)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.tab_4)).setOnCheckedChangeListener(this);

		((RadioButton) findViewById(R.id.tab_1)).setChecked(true);
	}

	private void initSlidingMenu() {
		mSlidingMenu = new SlidingMenu(this);

		mSlidingMenu.setBackgroundResource(R.drawable.left_fra_bg);
		mSlidingMenu.setMenu(R.layout.left_empty_layout);
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		mSlidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_MARGIN);

		int screenWidth = getResources().getDisplayMetrics().widthPixels;
		mSlidingMenu.setBehindWidth((int) (screenWidth * 0.7f));

		mSlidingMenu.setTranslateZoonMode(true);
		mSlidingMenu.setMode(SlidingMenu.LEFT);

		mSlidingMenu.attachToActivity(this, 0);
	}

	// 瀵笷ragment杩涜缂撳瓨
	private HashMap<Integer, Fragment> mFragmentCache = new HashMap<Integer, Fragment>();

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		// 褰撳墠鐨凴adioButton鏄寜涓嬬姸鎬佺殑鏃跺�锛屾墠鎵цFragment鐨勬浛鎹�		// 鍥犱负褰撲竴涓猂adioButton琚彇娑堥�涓殑鏃跺�锛屽悓鏍蜂細鍥炶皟onCheckedChanged鏂规硶
		if (isChecked) {

			Fragment fgm = mFragmentCache.get(buttonView.getId());

			if (fgm == null) {
				switch (buttonView.getId()) {
				case R.id.tab_1:
					JiuJiuFragment f = new JiuJiuFragment();
					f.setOnLayoutOpenListener(mLayoutOpenListener);
					fgm = f;
					break;
				case R.id.tab_2:
					fgm = new BuyFragment();
					break;
				case R.id.tab_3:
					fgm = new GoodFragment();
					break;
				case R.id.tab_4:
					fgm = new MeFragment();
					break;
				}

				mFragmentCache.put(buttonView.getId(), fgm);
			}

			getSupportFragmentManager().beginTransaction().replace(R.id.main_content, fgm).commit();
		}
	}

	private OnLayoutOpenListener mLayoutOpenListener = new OnLayoutOpenListener() {

		@Override
		public void openLayout() {
			mSlidingMenu.showMenu();
		}
	};

	public interface OnLayoutOpenListener {
		public void openLayout();
	}
}